/**
 * 
 */
package com.jeecms.wicool.dao.question.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;

import com.jeecms.common.hibernate3.Finder;
import com.jeecms.common.hibernate3.HibernateBaseDao;
import com.jeecms.common.page.Pagination;
import com.jeecms.wicool.dao.question.QuestionOptionDao;
import com.jeecms.wicool.entity.question.QuestionOption;

/**
 * @author spring
 *
 */
public class QuestionOptionDaoImpl extends
		HibernateBaseDao<QuestionOption, Integer> implements QuestionOptionDao {

	@Override
	protected Class<QuestionOption> getEntityClass() {
		return QuestionOption.class;
	}

	@Override
	public Pagination getPage(String questionId, String title, int pageNo,
			int pageSize) {
		 Finder f = Finder.create("select bean from QuestionOption bean where 1=1");

	        if (StringUtils.isNotBlank(title)) {
	            f.append(" and bean.title = :title");
	            f.setParam("title", title);
	        }
	        if (questionId != null) {
	            f.append(" and bean.questionId = :questionId");
	            f.setParam("questionId", questionId);
	        }
	        f.append(" order by bean.questionId desc,bean.id");
	        return find(f, pageNo, pageSize);
	}

	@Override
	public QuestionOption findById(Integer id) {
		return get(id);
	}

	@Override
	public QuestionOption save(QuestionOption bean) {
		getSession().save(bean);
		getSession().flush();
		getSession().clear();
		return bean;
	}

	@Override
	public QuestionOption deleteById(Integer id) {
		QuestionOption entity = get(id);
		if(entity != null){
			getSession().delete(entity);
		}
		return entity;
	}

	@Override
	public List<QuestionOption> deleteByIds(Integer[] ids) {
		List<QuestionOption> list = new ArrayList<QuestionOption>();
		for (Integer id : ids) {
			list.add(deleteById(id));
		}
		getSession().flush();
		getSession().clear();
		return list;
	}

}
